package com.game.majiang.vo;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.sql.rowset.CachedRowSet;

import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;

public class TableidconfigVo {

	public synchronized static int createtable() {
		int i=0;
		String cmdText = "select id,tableid from tableidconfig ";
		CachedRowSet rs = SqlHelper.getResultSet(cmdText);
		try {
			if (rs.next()) {
				i=rs.getInt(2);
				String cmdText1 = "select roomNumber from allgamelog order by id desc limit 0,1";
				CachedRowSet rs1 = SqlHelper.getResultSet(cmdText1);
				try {
					if (rs1.next()) {
						int old=rs1.getInt(1);
						if(old>=i) {
							i=old+1;
							Savetableid(i);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
	
	public  static void Savetableid(int tableid) {
		String sql = "UPDATE tableidconfig SET tableid="+tableid+"  WHERE  id=1";
		SqlHelperwrite.execute(sql);
	}
	
	public static void SaveIfoo(String ifoo) {
		String sql = "UPDATE tableidconfig SET ifoo='"+ifoo+"'  WHERE  id=1";
		SqlHelperwrite.execute(sql);
	}
	
	public static boolean Selectifoo() {
		Calendar cal = Calendar.getInstance();
		int oyear=cal.get(Calendar.YEAR);
		int omonth=cal.get(Calendar.MONTH)+1;
		int oday=cal.get(Calendar.DATE);
		String year = oyear+"";
		String month = omonth+"";
		if(month.length()<2) 
		{
			month="0"+month;
		}
		String day = oday+"";
		if (day.length()<2) {
			day = "0"+day;
		}
		boolean f=true;
		String cmdText1 = "select id from tableidconfig where id=1 and DATE_FORMAT(ifoo,'%Y%m%d') = '"+year+month+day+"'";
		CachedRowSet rs1 = SqlHelper.getResultSet(cmdText1);
		try {
			if (rs1.next()) {
				f=false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(f) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
			String endStr = simpleDateFormat.format(new Date());
			TableidconfigVo.SaveIfoo(endStr);
		}
		return f;
	}
	
	public static void insertDeletetableid(int tabeid) {
		String cmdText1 = "select tableid from deletetableid where tableid="+tabeid+"";
		CachedRowSet rs1 = SqlHelper.getResultSet(cmdText1);
		try {
			if (rs1.next()) {
				
			}else {
				 String sql = "INSERT INTO deletetableid (tableid) VALUES (?)";
				 Parameter p1 = new Parameter(Types.INTEGER, tabeid, ParameterType.Input);
				 SqlHelperwrite.execute(sql,p1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
